package com.pignest.hotel.ui.reception;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.CustomHistory;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class CustomsHistoryVbox extends Vbox implements EventListener {

	/**
	 * 客历
	 */
	private static final long serialVersionUID = 1643047117121433280L;
	private Paging pagCustoms;
	private Textbox qRoomName;// 房间名
	private Listbox qRoomTypeList;// 房间类型

	private Listbox qOpenStateList;
	private Textbox qName;// 客户姓名
	private Textbox qPhone;
	private Textbox qCerNo;
	private Textbox qCardno;

	private Datebox qStartLiveDate;// 入住开始日期
	private Datebox qEndLiveDate;// 入住截止日期

	private Listbox customsHistoryListbox;

	@Override
	public void onEvent(Event arg0) throws Exception {
		// TODO Auto-generated method stub

	}

	public void onCreate() {

		qRoomTypeList = (Listbox) getFellow("qRoomTypeList");
		qOpenStateList = (Listbox) getFellow("qOpenStateList");
		qName = (Textbox) getFellow("qName");
		qPhone = (Textbox) getFellow("qPhone");
		qCerNo = (Textbox) getFellow("qCerNo");
		qCardno = (Textbox) getFellow("qCardno");
		qStartLiveDate = (Datebox) getFellow("qStartLiveDate");
		qEndLiveDate = (Datebox) getFellow("qEndLiveDate");
		pagCustoms = (Paging) getFellow("pagCustoms");
		customsHistoryListbox = (Listbox) getFellow("customsHistoryListbox");
		final int PAGE_SIZE = pagCustoms.getPageSize();
		pagCustoms.addEventListener("onPaging", new EventListener() {
		      public void onEvent(Event event) {
		        	PagingEvent pe = (PagingEvent) event;
		        	int pgno = pe.getActivePage();
		        	int start = pgno*PAGE_SIZE;
		        	query(start,PAGE_SIZE);
		      	}
		    	});
			query(0,PAGE_SIZE);
	}

	public void query(int start, int pageSize) {
		customsHistoryListbox.getItems().clear();

		String sRoomTypeId = (String) UIUtils.getSelectedValue(qRoomTypeList);
		String sName = (String) qName.getValue();
		String sPhone = (String) qPhone.getValue();
		String sCerNo = (String) qCerNo.getValue();
		String sCardNo = (String) qCardno.getValue();
		String oType = (String) UIUtils.getSelectedValue(qOpenStateList);
		Date startLivingDate = qStartLiveDate.getValue();
		Date endLivingDate = qEndLiveDate.getValue();
		int Total = Services.getCustomService().queryCustomsNoPage(sName,
				sCerNo, null, null, sRoomTypeId, sPhone, sCardNo,oType,startLivingDate, endLivingDate).size();
		pagCustoms.setTotalSize(Total);
		List<CustomHistory> chList = Services.getCustomService().queryCustomHisPage(sName, sCerNo, null, null,
						sRoomTypeId, sPhone, sCardNo, oType,startLivingDate,
						endLivingDate, start, pageSize);
		int index=1;
		if(chList==null)
			return ;
		for(CustomHistory c:chList)
		{
			  Listitem listitem = new Listitem();
			  listitem.setValue(c.getId());
			  listitem.setParent(customsHistoryListbox);
			  listitem.appendChild(new Listcell(String.valueOf(index)));
			  listitem.appendChild(new Listcell(c.getName()));
			  listitem.appendChild(new Listcell(c.getSex()));
			  listitem.appendChild(new Listcell(c.getRoom().getName()));
			  listitem.appendChild(new Listcell(Utils.toPrice(c.getPrice())));
			  listitem.appendChild(new Listcell(Utils.toPrice(c.getRoomPrice())));
			  listitem.appendChild(new Listcell(c.getCertificateType()));
			  listitem.appendChild(new Listcell(c.getCertificateno()));
			  listitem.appendChild(new Listcell(c.getRoom().getType().getName()));
			  listitem.appendChild(new Listcell(OpenRoomType.valueOf(c.getOpenType()).getDisplayName()));
			  listitem.appendChild(new Listcell(DateUtil.toString(c.getLiveTime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			  listitem.appendChild(new Listcell(DateUtil.toString(c.getLeaveTime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			  if(c.getMember()!=null)
			     listitem.appendChild(new Listcell(c.getMember().getCardNo()));
			  else
				  listitem.appendChild(new Listcell("")); 
			  listitem.appendChild(new Listcell(c.getPhone()));
	
		}
	}
}
